package com.qding.park.manager.service.core.facade.impl.common;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.manager.facade.core.service.ConfDateFacade;
import com.qding.park.manager.pojo.SOAServiceEnum;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.TbSupplycomany;
import com.qding.park.manager.service.core.mapper.TbParkspaceMapper;
import com.qding.park.manager.service.core.mapper.TbSupplycomanyMapper;

@Service("confDateFacade")
public class ConfDataCommonFacadeImpl implements ConfDateFacade, ApplicationContextAware {
    private static final Logger _log = LoggerFactory.getLogger(ConfDataCommonFacadeImpl.class);
    // @Autowired
    // private ARKConfDataFacadeImpl arkConfDataFacade;
    //
    // @Autowired
    // private BSGConfDateFacadeImpl bsgConfDateFacadeImpl;

    protected ApplicationContext context;

    @Autowired
    private TbParkspaceMapper tbParkspaceMapper;

    @Autowired
    private TbSupplycomanyMapper tbSupplycomanyMapper;

    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        this.context = arg0;
    }

    protected <T> T getServiceObj(SOAServiceEnum serviceEnum, String supplyId, Class<T> t) {
        T resultObj = context.getBean(serviceEnum.getValue() + "_" + supplyId, t);
        return resultObj;
    }

    @Override
    public void confSync() throws BaseException {
        List<TbSupplycomany> tbSupplycomanys = tbSupplycomanyMapper.listAll();

        for (TbSupplycomany comany : tbSupplycomanys) {
            String supplierId = String.valueOf(comany.getId());
            ConfDateFacade confDataFacade = this.getServiceObj(SOAServiceEnum.BASEDATE_SYNC_SERVICE_NAME, supplierId,
                    ConfDateFacade.class);
            try {
                confDataFacade.confSync();
            } catch (Exception e) {
                _log.error(e.getMessage(), e);
            }
        }
    }

    @Override
    public void confSync(int parkId) throws BaseException {
        TbParkspace parkspace = tbParkspaceMapper.queryParkInfoByParkId(parkId);
        String supplierId = String.valueOf(parkspace.getSupplierId());
        ConfDateFacade confDataFacade = this.getServiceObj(SOAServiceEnum.BASEDATE_SYNC_SERVICE_NAME, supplierId,
                ConfDateFacade.class);

        confDataFacade.confSync(parkId);
    }

}
